package com.ezops.poc.routing;

import org.apache.camel.Predicate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
import org.apache.camel.dataformat.csv.CsvDataFormat;
import org.apache.camel.spi.DataFormat;
import org.springframework.stereotype.Component;

import com.ezops.poc.aggregation.EzopsAggregationStrategy;
import com.ezops.poc.bo.ot.ClientOTEquityModel;
import com.ezops.poc.parser.SingleClassBindyCsvDataFormat;
import com.ezops.poc.validations.EzopsValidationException;
import com.ezops.trade.processing.partners.orchestrade.equity.OrchesTradeEquityModel;

/**
 * Route builder class for configuring the routes in camel context.
 * 
 * @author vishal.gaurav
 * 
 */
@Component(value = "myBuilder")
public class TradeRoutingBuilder extends RouteBuilder {

	public static final String HEADER_KEY_IS_VALID_TRADE = "IS_VALID_TRADE";

	DataFormat jaxb = new JaxbDataFormat(
			"com.ezops.trade.processing.partners.calypsodocument");
	SingleClassBindyCsvDataFormat bindyOtEquity = new SingleClassBindyCsvDataFormat();
	BindyCsvDataFormat bindyOtModel = new BindyCsvDataFormat(
			OrchesTradeEquityModel.class);

	Predicate isValidTrade = header(HEADER_KEY_IS_VALID_TRADE).isEqualTo(true);

	/**
	 * COnfigre the routes.
	 */
	public void configure() {
		CsvDataFormat csv = new CsvDataFormat();
		bindyOtEquity.setModelClass(ClientOTEquityModel.class);

		onException(EzopsValidationException.class).processRef(
				"validationErrorProcessor").continued(true);// to("direct:errors");
		// only trigger when incoming from transformationBean endpoint
		interceptSendToEndpoint("bean:transformationBean?method=convert").to(
				"direct:persistData");

		// here is a sample which processes the input files
		// (leaving them in place - see the 'noop' flag)
		// then performs content based routing on the message using XPath
		from("properties:{{input.path}}")
				.unmarshal(csv)
				.processRef("clientFormatParser")
				.log(body().toString())
				.split(body(), new EzopsAggregationStrategy())
				// .split(body().tokenize("\n")).log("Line: ${body}")
				// .bindy(BindyType.Csv, "com.ezops.poc.bo")
				// .process(new LoggingProcessor())
				.to("bean:transformationBean?method=convert")
				// .to("drools:node1/ezopsKSession")
				.processRef("validationProcessor").choice().when(isValidTrade)
				.beanRef("destinationFormatConverter", "convert").otherwise()
				.to("bean:transformationBean?method=reverseConvert")
				.endChoice().end().end().processRef("loggingProcessor")
				.to("direct:output");
		// .to("properties:{{output.path}}")
		// .to("bean:batchExecutor?method=execute");
		// .to("rabbitmq://localhost:5672/Exchange_OrchestradeReportSender?connectionFactory=#customConnectionFactory&queue=Queue_OrchestradeReportCarrier");

		from("direct:output").processRef("splitProcessor");
		// split().method("tradeSplitter", "extractTokens");
		// .aggregate(header("CamelFileName"), new EzopsAggregationStrategy())
		// .completionTimeout(20000)
		// .processRef("splitProcessor");
		from("direct:errors")
				// .to("bean:transformationBean?method=reverseConvert")
				.marshal(bindyOtEquity).to("properties:{{output.errorpath}}")
				.stop();
		from("direct:validfile").marshal(bindyOtModel).to(
				"properties:{{output.path}}");

		from("direct:persistData").to("bean:clientDAO?method=persist");

		from(
				"rabbitmq://localhost:5672/TradeBooking?connectionFactory=#customConnectionFactory&routingKey=TradeCarrier&exchangeType=direct&durable=true&autoDelete=false")
				.to("properties:{{output.errorpath1}}");
	}
}
